function submitDetailForm()
{
  $("#detailRequestedAction").val('saveDetail');
  $('#detailForm').unbind('submit');
  $("#detailForm").submit();
}

function applyFilter()
{
  $("#detailRequestedAction").val('apply');
  $('#detailForm').unbind('submit');
  $("#detailForm").submit();
}

function submitClickedUrl(urlClicked)
{
  $("#detailRequestedAction").val('clickedUrl');
  $("#clickedUrl").val(urlClicked);
  $('#detailForm').unbind('submit');
  $("#detailForm").submit();
}

function clickedSearchButton()
{
  if (allowNavigationAwayDetail())
  {
    applyFilter();
  }
  else
  {
    openBeforeNavigationDialog('', 'false', 'false', 'true');
  }
}

function clickedTab(urlClicked)
{
  if (allowNavigationAwayDetail())
  {
    $('.unselectedTab').removeAttr('onClick');  
    submitNavigation(urlClicked, 'true', 'false');
  }
  else
  {
    openBeforeNavigationDialog(urlClicked, 'true', 'false', 'false');
  }
}

function fieldEdit(itemName)
{
  var newValueField = document.getElementById(itemName);
  var oldValueField = document.getElementById(itemName + '_originalValue'); 
  if (oldValueField.value == newValueField.value)
  {
	changeClassToFine(newValueField);
  }
  else
  {
	changeClassToChange(newValueField);
	
  }
}

function fieldEditCheckBox(itemName)
{
  var newValueField = document.getElementById(itemName);
  var oldValueField = document.getElementById(itemName + '_originalValue');
  if ((oldValueField.value == 'true' && newValueField.checked)
	  || (oldValueField.value == 'false' && !newValueField.checked))
  {
	changeClassToFine(newValueField);
  }
  else
  {
	changeClassToChange(newValueField);
  }
}

function bookmarkedFieldEdit(item)
{
  var newValueField = item;
  var oldValueField = document.getElementById('summaryBox.bookmarked_originalValue');
  if (oldValueField.value == newValueField.checked)
  {
  changeClassToFine(newValueField);
  }
  else
  {
    var currentClassName = newValueField.className;
    if (currentClassName.indexOf("change") != -1 || currentClassName.indexOf("error") != -1)
    {
    if (currentClassName.indexOf("_") != -1)
      {
        field.className = currentClassName.substring(0, currentClassName.indexOf("_"));
      }
      else
      {
        field.className = currentClassName;
      }
    }
  }
}

function afterFieldEdit(messageCode,
		                typeForValidation,
		                itemName,
		                allowNull,
		                excludedValues,
		                maxPrecision,
		                maxScale)
{
  var newValueField = document.getElementById(itemName);

  if (maxPrecision == null || maxPrecision == "")
  {
    maxPrecision = -1;
  }

  if (maxScale == null || maxScale == "")
  {
    maxScale = -1;
  }

  var strMaxPrec = maxPrecision.toString(10);
  var strMaxScle = maxScale.toString(10);
  var excValues = JSON.stringify(excludedValues);
  
  $.ajax({url: 'validation.htm', type: 'POST',
	     data : {msgCode : messageCode, 
		    	valType : typeForValidation, 
		 	    newVal : newValueField.value,
		 	    strMaxPrecision : strMaxPrec,
		 	    strMaxScale : strMaxScle, 
		 	    allowNullFlag : allowNull,
		 	    excValues : excValues,
	    	    adminRequestedAction:"validateField"}}).success( function (aRes) {    errorMessage = aRes;
																		    		  if (errorMessage != "")
																		    		  {
																		    		    changeClassToError(newValueField);
																		    		    addError(itemName, errorMessage);
																		    		  }
																		    		  else
																		    		  {
																		    		    fieldEdit(itemName);
																		    		    removeError(itemName);
																		    		  }
	    		  													});
}

function addError(itemName, errorMessage)
{
  var itemId = parseIdForJQuery(itemName);
  if ($(itemId + "_errorField").length < 1)
  {
	$("#errorsTable").css("display", "block");
	$("#errorCount").val(parseInt(parseInt($("#errorCount").val()) + parseInt(1)));
	$("#errorsTable").html($("#errorsTable").html() + "<tr id='" + itemName + "Row' class='errorText'><td>" + errorMessage + "</td>");
	disableButton("saveButton");
  }
  else
  {
	if ($(itemId + "_errorField").css("visibility") == "hidden")
	{
	  $("#errorCount").val(parseInt(parseInt($("#errorCount").val()) + parseInt(1)));
	  $(itemId + "_errorField").css("visibility", "visible").css("display" , "block").html(errorMessage);
      $(itemId).attr("title", errorMessage);
	  disableButton("saveButton");
    }
  }
}

function removeError(itemName)
{
  var itemId = parseIdForJQuery(itemName);
  if ($(itemId + "_errorField").length < 1)
  {
	if ($("#errorCount").val() > 0)
	{
	  $("#errorCount").val(parseInt(parseInt($("#errorCount").val()) - parseInt(1)));
	}
	$("#errorsTable > #" + itemName + "Row").remove();

	if ($("#errorCount").val() == 0)
	{
      enableSaveButton();
	}
  }
  else
  {
	if ($(itemId + "_errorField").css("visibility") == "visible")
	{
	  if ($("#errorCount").val() > 0)
	  {
		$("#errorCount").val(parseInt(parseInt($("#errorCount").val()) - parseInt(1)));
	  }

	  $(itemId + "_errorField").css("visibility", "hidden").css("display" , "none").html("");
      $(itemId).attr("title", "");
	  if ($("#errorCount").val() == 0)
	  {
		enableSaveButton();
	  }
	}
  }
}

function stripItemName(itemName)
{
  if (itemName.indexOf("_error") > 0)
  {
    return itemName.substring(0, itemName.indexOf("_error"));
  }
  return itemName;
}

function changeClassToFine(field)
{
  var currentClassName = field.className;
  if (currentClassName.indexOf("change") != -1 || currentClassName.indexOf("error") != -1)
  {
    if (currentClassName.indexOf("_") != -1)
    {
      field.className = currentClassName.substring(0, currentClassName.indexOf("_"));
    }
    else
    {
      field.className = currentClassName;
    }

    if ($("#changeCount").val() > 0)
    {
      $("#changeCount").val(parseInt($("#changeCount").val()) - 1);
    }
    checkButtons();
  }
}

function changeClassToChange(field)
{
  var currentClassName = field.className;
  if (currentClassName.indexOf("change") == -1)
  {
    if (currentClassName.indexOf("_") != -1)
    {
      field.className = currentClassName.substring(0, currentClassName.indexOf("_")) + '_change';
    }
    else
    {
      if (currentClassName.indexOf(' ') != -1)
      {
    	field.className = currentClassName.replace(" ", "_change ");
      }
      else
      {
        field.className = currentClassName + '_change';
      }
    }

    $("#changeCount").val(parseInt($("#changeCount").val()) + 1);
    checkButtons();
  }
}

function changeClassToError(field)
{
  var currentClassName = field.className;

  if (currentClassName.indexOf("_") != -1)
  {
    field.className = currentClassName.substring(0, currentClassName.indexOf("_")) + '_error';
  }
  else
  {
    if (currentClassName.indexOf(' ') != -1)
    {
      field.className = currentClassName.replace(" ", "_error ");
    }
    else
    {
      field.className = currentClassName + '_error';
    }
  }
}

function clearChanges()
{
  var location = "" + window.location;
  if (location.indexOf("contactDetailsEdit.htm") >= 0)
  {
    clearChangesContactDetailEdit();
    return;
  }

  $("#saveButton").attr("title", "");

  var originalFieldId, originalField;
  $("input").each(function() {
    originalFieldId = $(this).attr('id') + "_originalValue";
    originalField = document.getElementById(originalFieldId);

    if (originalField != null)
    {
      $(this).val(originalField.value);
      $(this).change();
    }
  });

  $("select").each(function() {
    originalFieldId = $(this).attr('id') + "_originalValue";
    originalField = document.getElementById(originalFieldId);

    if (originalField != null)
    {
    $(this).val(originalField.value);
    $(this).change();
    }
  });


  $("textarea").each(function() {
    originalFieldId = $(this).attr('id') + "_originalValue";
    originalField = document.getElementById(originalFieldId);

    if (originalField != null)
    {
      $(this).val(originalField.value);
      $(this).change();
      document.getElementById($(this).attr('id') + "_remainingCharacters").innerHTML = "";
    }

  });

  $("#changeCount").val(0);
  checkButtons();
}

function checkButtons()
{
  if ($("#changeCount").val() == 0) // Disable Buttons
  {
	disableButton('saveButton');
    disableButton('clearChangesButton');
  }
  else  // Enable Buttons
  {
    if ($("#errorCount").val() == 0)
    {
      enableSaveButton();
    }
    enableClearChangesButton();
  }
}

function enableSaveButton()
{
  var button = $("#saveButton");
  if (button != null)
  {
	button.unbind('click');
    button.click(function() {this.blur();openSaveChangesDialog();});
    button.unbind('mouseover');
    button.mouseover(function() {buttonHover(this);});
    button.unbind('mouseout');
    button.mouseout(function() {buttonNormal(this);});
    button.addClass("button_input").removeClass("button_inputDisabled");
    button.attr("title", "");
    button.keypress(function() {enterKeyPress(event);});
  }
}

function enableClearChangesButton()
{
  var button = $("#clearChangesButton");
  if (button != null)
  {
    button.unbind('click');
    button.click(function() {this.blur();openClearChangesDialog();});
    button.unbind('mouseover');
    button.mouseover(function() {buttonHover(this);});
    button.unbind('mouseout');
    button.mouseout(function() {buttonNormal(this);});
    button.addClass("button_input").removeClass("button_inputDisabled").removeClass("button_inputHover");
  }
}

function disableButton(name)
{
  var button = $("#" + name);
  if (button != null)
  {
    button.unbind('click');
    button.unbind('mouseover');
    button.unbind('mouseout');
    button.addClass("button_inputDisabled").removeClass("button_input");

    if (name == "saveButton")
    {
      if ($("#errorCount").val() == 0)
      {
    	button.attr("title", "");
      }
      else
      {
        button.attr("title", "An update can only be carried out when all changes are valid.");
      }
    }
  }
}

function textAreaCounter(textAreaElement, maxLength)
{
  var label = document.getElementById(textAreaElement.id + "_remainingCharacters");
  if (textAreaElement.value.length > maxLength)
  {
    textAreaElement.value = textAreaElement.value.substring(0, maxLength);
  }
  else
  {
    label.innerHTML = '(' + (maxLength - textAreaElement.value.length) + ' characters available)';
  }
}

function enterKeyPress(e)
{
  if (window.event)
  {
    e = window.event;
  }

  if (e.keyCode == 13)
  {
    document.getElementById('saveButton').click();
  }
}